<!-- 文章链接 -->
<!-- https://mp.weixin.qq.com/s/O3UKTzQaRpHa--yuio2uZA -->
<!-- ES2015+ ，callback (async.js) -> Promise -> generator + co -> async +   -->
<!-- 当前：基于 Promise + async/await -->
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
    
</body>
<!-- <script>
  // 初级写法
  function A(callback) {
    ajax(url, function(res) {
      callback(res)
    })
  }
  function B(callback) {
    ajax(url, function(res) {
      callback(res)
    })
  }
  function C(callback) {
    ajax(url, function(res) {
      callback(res)
    })
  }
  function D(callback) {
    ajax(url, function(res) {
      callback(res)
    })
  }
</script> -->

<script>
  function A() {
    return new Promise(r => {
      let timeObj = setTimeOut(() => {
        clearTimeout(timeObj)
        r("A")
      }, 2000)
    })
  }
  function B() {
    return new Promise(r => {
      let timeObj = setTimeOut(() => {
        clearTimeout(timeObj)
        r("B")
      }, 2000)
    })
  }
  function C(resb) {
    return new Promise(r => {
      let timeObj = setTimeOut(() => {
        clearTimeout(timeObj)
        r("C", resb)
      }, 2000)
    })
  }
  function D() {
    return new Promise(r => {
      let timeObj = setTimeOut(() => {
        clearTimeout(timeObj)
        r("D")
      }, 2000)
    })
  }
  async function asyncBC() {
    const resb = await B();
    const resC = await C(resb);
    return { resb, resC}
  }

  async function asyncTask() {
    const [resa, { resb, resC}] = await Promise.all([A(), asyncBC()])
    const resd = await D(resa, resb, resC)
    return resd
  }

  asyncTask().then(resd => {
    console.log("this is D result: ", resd)
  })
</script>
</html>